﻿Public Class QuanLyDoiTac


    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub QuanLyDoiTac_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDGV()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim DoiTacDAO As New DoiTacDAO
        Dim dt As New DoiTacDTO
        dt.MaDoiTac = txtMaDT.Text
        dt.TenDoiTac = txtTenDT.Text
        dt.DiaChi = txtDiaChi.Text
        dt.MST = txtMaST.Text
        dt.KiHieuHoaDon = txtKHHD.Text
        dt.NguoiLienLac = txtNguoiLienLac.Text
        dt.Email = txtEmail.Text
        dt.DienThoai = txtDienThoai.Text
        If DoiTacDAO.Insert(dt) = 1 Then
            MsgBox("Thêm thành công", MsgBoxStyle.OkOnly, "")
        Else
            MsgBox("Thêm không thành công", MsgBoxStyle.OkOnly, "")
        End If
        LoadDGV()
        Clear()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim _maDT As String = txtMaDT.Text
        Dim DoiTacDAO As New DoiTacDAO
        If (DoiTacDAO.Delete(_maDT)) = 1 Then
            MsgBox("Xóa thành công", MsgBoxStyle.OkOnly, "")
        Else
            MsgBox("Xóa không thành công", MsgBoxStyle.OkOnly, "")
        End If
        LoadDGV()
        Clear()
    End Sub

    'Private Sub dgvDSDT_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDSDT.SelectionChanged
    '    If (dgvDSDT.SelectedRows.Count > 0) Then
    '        txtMaDT.Text = dgvDSDT.SelectedRows(0).Cells(0).Value.ToString()
    '        txtTenDT.Text = dgvDSDT.SelectedRows(0).Cells(1).Value.ToString()
    '        txtDiaChi.Text = dgvDSDT.SelectedRows(0).Cells(2).Value.ToString()
    '        txtMaST.Text = dgvDSDT.SelectedRows(0).Cells(3).Value.ToString()
    '        txtKHHD.Text = dgvDSDT.SelectedRows(0).Cells(4).Value.ToString()
    '        txtDienThoai.Text = dgvDSDT.SelectedRows(0).Cells(5).Value.ToString()
    '        txtNguoiLienLac.Text = dgvDSDT.SelectedRows(0).Cells(6).Value.ToString()
    '        txtEmail.Text = dgvDSDT.SelectedRows(0).Cells(1).Value.ToString()
    '    End If
    'End Sub

    Private Sub LoadDGV()
        Dim DoiTacDAO As New DoiTacDAO
        Dim dt As DataTable
        dt = DoiTacDAO.Select_All()
        dgvDSDT.DataSource = dt
    End Sub

    Private Sub dgvDSDT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDSDT.Click
        If (dgvDSDT.SelectedRows.Count > 0) Then
            Clear()
            txtMaDT.Text = dgvDSDT.SelectedRows(0).Cells(0).Value.ToString()
            txtTenDT.Text = dgvDSDT.SelectedRows(0).Cells(1).Value.ToString()
            txtDiaChi.Text = dgvDSDT.SelectedRows(0).Cells(2).Value.ToString()
            txtMaST.Text = dgvDSDT.SelectedRows(0).Cells(3).Value.ToString()
            txtKHHD.Text = dgvDSDT.SelectedRows(0).Cells(4).Value.ToString()
            txtDienThoai.Text = dgvDSDT.SelectedRows(0).Cells(5).Value.ToString()
            txtNguoiLienLac.Text = dgvDSDT.SelectedRows(0).Cells(6).Value.ToString()
            txtEmail.Text = dgvDSDT.SelectedRows(0).Cells(7).Value.ToString()
        End If
    End Sub

    Private Sub Clear()
        txtMaDT.Text = ""
        txtTenDT.Text = ""
        txtDiaChi.Text = ""
        txtMaST.Text = ""
        txtDienThoai.Text = ""
        txtKHHD.Text = ""
        txtNguoiLienLac.Text = ""
        txtEmail.Text = ""
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Dim DoiTacDAO As New DoiTacDAO
        Dim dt As New DoiTacDTO
        dt.MaDoiTac = txtMaDT.Text
        dt.TenDoiTac = txtTenDT.Text
        dt.DiaChi = txtDiaChi.Text
        dt.MST = txtMaST.Text
        dt.KiHieuHoaDon = txtKHHD.Text
        dt.NguoiLienLac = txtNguoiLienLac.Text
        dt.Email = txtEmail.Text
        dt.DienThoai = txtDienThoai.Text
        If (DoiTacDAO.Update(dt)) = 1 Then
            MsgBox("Cập nhật thành công", MsgBoxStyle.OkOnly, "")
        Else
            MsgBox("Cập nhật không thành công", MsgBoxStyle.OkOnly, "")
        End If
        LoadDGV()
        Clear()
    End Sub

  
    Private Sub btnTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTim.Click
        Dim DoiTacDAO As New DoiTacDAO
        Dim dt As DataTable
        Dim _maDT As String
        _maDT = txtMaDT.Text
        dt = DoiTacDAO.Select_By_Ma(_maDT)
        If (dt.Rows.Count > 0) Then
            dgvDSDT.DataSource = dt
        Else
            MsgBox("Không tìm thấy đối tác", MsgBoxStyle.OkOnly, "")
        End If
    End Sub
End Class